package com.webex.ta.hydra.function;

import com.webex.ta.hydra.exceptions.InvalidVariableException;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-19
 * Time: 12:25:26
 */
public class IntSum extends AbstractFunction {
    private static final List<String> desc = new LinkedList<String>();

	private static final String KEY = "__intSum";

	static {
		desc.add("First int to add.");
		desc.add("Second int to add");
	}

    private Object[] values;

    @Override
    public String getReferenceKey() {
        return KEY;
    }

    public List<String> getArgumentDesc() {
        return desc;
    }


    @Override
    public void setParameters(Collection parameters) throws InvalidVariableException {
        values = parameters.toArray();

		if (values.length < 2) {
			throw new InvalidVariableException();
		}
    }

    @Override
    public String execute() {
        int sum = 0;
        for (Object value : values) {
            sum += Integer.parseInt(((CompoundVariable) value).execute());
        }

		return Integer.toString(sum);
    }
}
